package mysql

import (
	"novel/novelrpc/novelrpc"

	"gorm.io/gorm"
)

type Bookshelf struct {
	gorm.Model
	UserId  int64 `gorm:"type:int(11);not null"`
	NovelId int64 `gorm:"type:int(11);not null"`
}

func (b *Bookshelf) FirstBookshelf(db *gorm.DB, in *novelrpc.BookShelfAddRequest) error {
	return db.Debug().Where("user_id", in.UserId).Where("novel_id", in.NovelId).Find(b).Error
}

func (b *Bookshelf) BookSelfAdd(db *gorm.DB) error {
	return db.Debug().Create(b).Error
}

func (b *Bookshelf) FindBookshelf(db *gorm.DB, UserId int64, NovelId int64) error {
	return db.Debug().Where("user_id=?", UserId).Where("novel_id=?", NovelId).Find(b).Error
}

func (b *Bookshelf) BookSelfDel(db *gorm.DB, UserId int64, NovelId int64) error {
	return db.Debug().Where("user_id=?", UserId).Where("novel_id=?", NovelId).Delete(b).Error
}

func BookSelfShow(db *gorm.DB, in *novelrpc.BookShelfShowRequest) []*novelrpc.BookShelfShow {
	var show []*novelrpc.BookShelfShow
	/*
		SELECT
		  bookshelves.id
		FROM
		  bookshelves
		  LEFT JOIN novels ON bookshelves.novel_id = novels.id
	*/
	db.Debug().Model(&Bookshelf{}).Select(" bookshelves.id as bookshelfId,novels . * ,bookshelves.user_id").
		Joins(" LEFT JOIN novels ON bookshelves.novel_id = novels.id").
		Where("bookshelves.user_id =?", in.UserId).
		Find(&show)
	return show

}
